package vn.vnvas.xoso.mienbac.tuongthuat;

import org.json.JSONArray;
import org.json.JSONObject;

import vn.vnvas.xoso.R;
import vn.vnvas.xoso.utils.WebService_TuongThuat;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class tab_tuongthuat extends Activity implements OnClickListener{

	private Handler mHandler;
	private WebService_TuongThuat mService_TuongThuat;
	private String jString, ngay, giaiDB, giai1, giai2, giai3, giai4, giai5,
			giai6, giai7;

	private JSONObject mObject;
	private JSONArray mArray;

	private TextView tvThongbao,tvThongbao2, tvDay, tvGiai1, tvGiai2, tvGiai3, tvGiai4,
			tvGiai5, tvGiai6, tvGiai7, tvGiaiDB;
	private LinearLayout layoutThongBao;

	private boolean isRun = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_tuongthuat);

		tvThongbao = (TextView) findViewById(R.id.tvThongbao1);
		tvThongbao2 = (TextView) findViewById(R.id.tvThongbao2);
		tvDay = (TextView) findViewById(R.id.tvDayTuongThuat);
		tvGiai1 = (TextView) findViewById(R.id.tvGiai1TuongThuat);
		tvGiai2 = (TextView) findViewById(R.id.tvGiai2TuongThuat);
		tvGiai3 = (TextView) findViewById(R.id.tvGiai3TuongThuat);
		tvGiai4 = (TextView) findViewById(R.id.tvGiai4TuongThuat);
		tvGiai5 = (TextView) findViewById(R.id.tvGiai5TuongThuat);
		tvGiai6 = (TextView) findViewById(R.id.tvGiai6TuongThuat);
		tvGiai7 = (TextView) findViewById(R.id.tvGiai7TuongThuat);
		tvGiaiDB = (TextView) findViewById(R.id.tvGiaiDBTuongThuat);
		
		layoutThongBao = (LinearLayout)findViewById(R.id.layoutThongBao);

		mService_TuongThuat = new WebService_TuongThuat();
		mHandler = new Handler();
		isRun=true;
		mHandler.post(mRunable);

	}

	private final Runnable mRunable = new Runnable() {

		@Override
		public void run() {
			jString = mService_TuongThuat
					.WsGETKETQUA("Xsmb_TuongThuatTrucTiep");
			if (jString
					.equals("\"\"")) {
				LinearLayout.LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
				layoutThongBao.setLayoutParams(lp);
				tvThongbao.setText("Hiện tại chưa có kết quả");
				tvThongbao2.setText("Vui lòng vào lại khi đến giờ quay!");
				
			} else {
				LinearLayout.LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, 0);
				layoutThongBao.setLayoutParams(lp);
				try {
					mArray = new JSONArray(jString);
					if (mArray.length() > 0) {
						mObject = mArray.getJSONObject(0);

						ngay = mObject.getString("LotTime").substring(0, 10);
						;
						String[] ngay_temp = ngay.split("-");
						ngay = "KQXS ngày " + ngay_temp[2] + "-" + ngay_temp[1]
								+ "-" + ngay_temp[0];
						tvDay.setText(ngay);

						giaiDB = mObject.getString("GiaiDB");
						tvGiaiDB.setText(giaiDB);

						giai1 = mObject.getString("Giai1");
						tvGiai1.setText(giai1);
						giai2 = mObject.getString("Giai2");
						tvGiai2.setText(giai2);
						giai3 = mObject.getString("Giai3");
						tvGiai3.setText(giai3);
						giai4 = mObject.getString("Giai4");
						tvGiai4.setText(giai4);
						giai5 = mObject.getString("Giai5");
						tvGiai5.setText(giai5);
						giai6 = mObject.getString("Giai6");
						tvGiai6.setText(giai6);
						giai7 = mObject.getString("Giai7");
						tvGiai7.setText(giai7);
					}

				} catch (Exception e) {
					// TODO: handle exception
				}
			}
			if (isRun&&tvGiaiDB.getText().toString().equals("")) {
				mHandler.postDelayed(mRunable, 5000);
			}
		}
	};

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		isRun=false;
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		isRun=true;
		mHandler.post(mRunable);
	}
	
}
